function loadStudents() {
    $.ajax({
        //url: '/admin/filter/getstudentbylab/codigo_laboratorio/' + $('#codigo_laboratorio').val(),
        url: '/admin/filter/getstudentbylab',
        dataType: 'json',
        type: 'post',
        data: {
            'codigo_laboratorio': $('#codigo_laboratorio').val(),
            'codigo_vm': $("#codigo_vm").val()
        },
        success: function(data) {
            $('#student-from').html(data.from);            
            $('#student-to').html(data.to);            
        }
    });
}

function addRemoveStudent()
{
    $('#btn-add-student').click(function() {
        $('#student-from option:selected').each(function() {
            $('#student-to').append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option>");
            $(this).remove();
        });
    });

    $('#btn-remove-student').click(function() {
        $('#student-to option:selected').each(function() {
            $('#student-from').append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option>");
            $(this).remove();
        });
    });
}

function savechanges() {
    bootbox.confirm("Desea guardar los cambios?", function(result) {
        if(result) {
            //Guardamos los cambios
            var studentAssigned = new Array();
            var i = 0;
            $('#student-to option').each(function() {
                studentAssigned [i++] = ($(this).val());
            });                        
            $.ajax({
                url:'/admin/virtualmachine/vmassignsave',
                //dataType: 'json',
                type: 'post',
                dataType: 'json',
                data : {
                    'students': studentAssigned,
                    'codigo_vm': $("#codigo_vm").val()
                },
                success: function(data) {
                    if(data.response === "success") {
                        comeback();
                    } else {
                        alert('Ocurrio un Error Inesperado: ' + data.message);
                    }
                }
            });            
        }
    }); 
}

function comeback() {
    window.history.back();
}

$(document).ready(function() {
    menuClass('laboratorio');
    loadStudents();
    addRemoveStudent();
});